package sgi.client;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;

import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilDateModel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.SpringLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SGISearchArchiveUI extends JFrame {

	private JPanel contentPane;
	private static SGISearchArchiveController searchController;
	private JTextField textFieldLocation;
	private SpringLayout springLayout;
	JDatePickerImpl datePickerFrom;
	JDatePickerImpl datePickerTo;
	
	public SGISearchArchiveUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 526, 525);
		setTitle("SGI Search - " + ((SGIUserAccountMenuController.create(null)).getUser()).getUsername());
		contentPane = new JPanel();
		contentPane.setLayout(null);
		setContentPane(contentPane);
		
		JLabel lblLocation = new JLabel("Location:");
		lblLocation.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblLocation.setBounds(10, 26, 62, 14);
		contentPane.add(lblLocation);
		
		textFieldLocation = new JTextField();
		textFieldLocation.setBounds(66, 24, 117, 20);
		contentPane.add(textFieldLocation);
		textFieldLocation.setColumns(10);
		
		UtilDateModel model = new UtilDateModel();
		JDatePanelImpl datePanel = new JDatePanelImpl(model);
		datePickerFrom = new JDatePickerImpl(datePanel);
		springLayout.putConstraint(SpringLayout.NORTH, datePickerFrom.getJFormattedTextField(), 0, SpringLayout.NORTH, datePickerFrom);
		springLayout.putConstraint(SpringLayout.WEST, datePickerFrom.getJFormattedTextField(), -90, SpringLayout.EAST, datePickerFrom);
		springLayout.putConstraint(SpringLayout.SOUTH, datePickerFrom.getJFormattedTextField(), 23, SpringLayout.NORTH, datePickerFrom);
		springLayout.putConstraint(SpringLayout.EAST, datePickerFrom.getJFormattedTextField(), -35, SpringLayout.EAST, datePickerFrom);
		springLayout = (SpringLayout) datePickerFrom.getLayout();
		datePickerFrom.setBounds(388, 23, 109, 31);
		contentPane.add(datePickerFrom);
		
		UtilDateModel model1 = new UtilDateModel();
		JDatePanelImpl datePanel1 = new JDatePanelImpl(model1);
		datePickerTo = new JDatePickerImpl(datePanel1);
		springLayout.putConstraint(SpringLayout.NORTH, datePickerTo.getJFormattedTextField(), 0, SpringLayout.NORTH, datePickerTo);
		springLayout.putConstraint(SpringLayout.WEST, datePickerTo.getJFormattedTextField(), -90, SpringLayout.EAST, datePickerTo);
		springLayout.putConstraint(SpringLayout.SOUTH, datePickerTo.getJFormattedTextField(), 23, SpringLayout.NORTH, datePickerTo);
		springLayout.putConstraint(SpringLayout.EAST, datePickerTo.getJFormattedTextField(), -35, SpringLayout.EAST, datePickerTo);
		springLayout = (SpringLayout) datePickerTo.getLayout();
		datePickerTo.setBounds(247, 23, 109, 31);
		contentPane.add(datePickerTo);
		
		JLabel lblFrom = new JLabel("From:");
		lblFrom.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblFrom.setBounds(209, 26, 42, 14);
		contentPane.add(lblFrom);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				java.sql.Date selectedDateFrom = (java.sql.Date) datePickerFrom.getModel().getValue();
				java.sql.Date selectedDateTo = (java.sql.Date) datePickerTo.getModel().getValue();
				if(textFieldLocation.getText() != "")
					searchController.search((selectedDateFrom.getYear()+1900)+"-"+(selectedDateFrom.getMonth()+1)+"-"+(selectedDateFrom.getDay()+1),(selectedDateTo.getYear()+1900)+"-"+(selectedDateTo.getMonth()+1)+"-"+(selectedDateTo.getDay()+1));
				else
					searchController.search((selectedDateFrom.getYear()+1900)+"-"+(selectedDateFrom.getMonth()+1)+"-"+(selectedDateFrom.getDay()+1),(selectedDateTo.getYear()+1900)+"-"+(selectedDateTo.getMonth()+1)+"-"+(selectedDateTo.getDay()+1),textFieldLocation.getText());
				
			}
		});
		btnSearch.setBounds(10, 65, 89, 23);
		contentPane.add(btnSearch);
		
		JList list = new JList();
		list.setBounds(10, 105, 487, 337);
		contentPane.add(list);
		
		JButton btnPuchaseImage = new JButton("Puchase Image");
		btnPuchaseImage.setBounds(239, 65, 117, 23);
		contentPane.add(btnPuchaseImage);
		
		JButton btnGetInfo = new JButton("Get Info");
		btnGetInfo.setBounds(126, 65, 89, 23);
		contentPane.add(btnGetInfo);
		
		JButton btnBack = new JButton("Back");
		btnBack.setBounds(408, 453, 89, 23);
		contentPane.add(btnBack);
		
		JButton btnPurchaseSample = new JButton("Purchase Sample");
		btnPurchaseSample.setBounds(382, 65, 115, 23);
		contentPane.add(btnPurchaseSample);
		
		JLabel lblTo = new JLabel("To:");
		lblTo.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblTo.setBounds(366, 27, 29, 14);
		contentPane.add(lblTo);
		
	}
	
	public void setController(SGISearchArchiveController search)
	{
		SGISearchArchiveUI.searchController = search;
	}
}
